/**
 * 
 */
package com.txzn.common.question;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.txzn.common.config.common.config.EnumMsg;
import com.txzn.common.model.Question;

/**
 * 试题维护的相关接口
 * @author hadoop
 *
 */
public class QuestionController extends Controller {

	
	public void index(){
	
		List<Question> questions = Question.dao.find("select * from xt_question order by qid desc ");
		
		renderJson(Ret.ok().set("list",questions).set("msg", EnumMsg.success.getValue()));
		
	}
	/**
	 * 题目列表
	 */
	public void list(){
		
		List<Question> questions = Question.dao.find("select * from xt_question order by qid desc ");
		
		renderJson(Ret.ok().set("list",questions).set("msg", EnumMsg.success.getValue()));
		
	}
	/**
	 * 新增
	 */
	public void add(){
	
		Question question = getModel(Question.class, "question");
		
		Ret ret = new Ret();
		
		boolean flag = question.save();
		
		if(flag){
			ret = Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
		}else{
			ret = Ret.fail().set("obj",question).set("msg", EnumMsg.fail.getValue());;
		}
		renderJson(ret);
		
	}
	/**
	 * 删除
	 */
	public void del(){
		Question question = Question.dao.findById(getPara());
		
		Ret ret = new Ret();
		
		boolean flag = question.delete();
		
		if(flag){
			ret = Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
		}else{
			ret = Ret.fail().set("obj",question).set("msg", EnumMsg.fail.getValue());;
		}
		renderJson(ret);
		
	}
	/**
	 * 修改
	 */
	public void edit(){
	
		Question question = getModel(Question.class, "question");
		
		Ret ret = new Ret();
		
		boolean flag = question.update();
		
		if(flag){
			ret = Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
		}else{
			ret = Ret.fail().set("obj",question).set("msg", EnumMsg.fail.getValue());;
		}
		renderJson(ret);
		
	}
	/**
	 * 根据id查询题目
	 */
	public void question(){
		
		Question question = Question.dao.findById(getPara());
			

		Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
	
		renderJson(Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue()));
		
	}
	/**
	 * 选过的的题目更新状态，
	 */
	public void updIsShow(){
		
		Question question = Question.dao.findById(getPara());
		
		question.setIsShow("1");
		
		Ret ret = new Ret();
		
		boolean flag = question.update();
		
		if(flag){
			ret = Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
		}else{
			ret = Ret.fail().set("obj",question).set("msg", EnumMsg.fail.getValue());;
		}
		renderJson(ret);
		
	}
	/**
	 * 题目更新状态，
	 */
	public void _updIsShow(){
		
		Question question = Question.dao.findById(getPara());
		
		question.setIsShow(getPara("isshow","0"));
		
		Ret ret = new Ret();
		
		boolean flag = question.update();
		
		if(flag){
			ret = Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
		}else{
			ret = Ret.fail().set("obj",question).set("msg", EnumMsg.fail.getValue());;
		}
		renderJson(ret);
		
	}
	/**
	 * 保存题目答题时间
	 * 
	 */
	public void updUseTime(){
		
		Question question = Question.dao.findById(getPara("id"));
		
		question.setUseTime(getPara("time"));
		
		Ret ret = new Ret();
		
		boolean flag = question.update();
		
		if(flag){
			ret = Ret.ok().set("obj",question).set("msg", EnumMsg.success.getValue());
		}else{
			ret = Ret.fail().set("obj",question).set("msg", EnumMsg.fail.getValue());;
		}
		renderJson(ret);
		
	}
	
}
